//
//  HZ_Find_ViewController.m
//  HZ_Beautiful_Music
//
//  Created by dllo on 16/3/30.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "HZ_Find_ViewController.h"
#import "HZ_Find_Model.h"
#import "AppDelegate.h"

#import "HZ_Find_CollectionViewCell.h"
#import "HZJRecommendCVCell.h"

#import "HZJTuijianModel.h"
#import "HS_DataArray_Model.h"
#import "HZ_songs_Model.h"
#import "HZJTuijianModel.h"
#import "CSL_MV_dataModel.h"
#import "Search_MV_Model.h"
#import "Search_MV_dataModel.h"
#import "Search_MV_Data_mvListModel.h"

#import "HZ_PrefixHeader.pch"
#import "HS_Billboard_ViewController.h"
#import "HZ_playlist_ViewController.h"
#import "CSL_Player_ViewController.h"

#import "HZJ_SingerViewController.h"
#import "HZ_Radio_ViewController.h"
#import "HZ_more_ViewController.h"

#import "HZ_Wab_ViewController.h"
#import "HZ_jibuqiCollectionViewCell.h"
#import "CSL_MoreViewController.h"
#import "HZJPlayListDetailViewController.h"
#import "CSL_PlayerPage_ViewController.h"
@interface HZ_Find_ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, JRecommendSend, singerAction>

@property (nonatomic, strong) UISegmentedControl *seg;
@property (nonatomic, strong) UICollectionView *coll;

/**
 *  下方为推荐页面所需
 */
@property (nonatomic, strong) NSMutableArray *tuijianDataSource;
@property (nonatomic, strong) NSMutableArray *discoveryArray;
@property (nonatomic, strong) HZ_Find_Model *findModel;
@property (nonatomic, strong) HS_DataArray_Model *dataModel;


@property (nonatomic, copy) NSString *string;
@property (nonatomic, strong) CSL_Data_MvListModel *listModel;
@property (nonatomic, copy) NSString *urlString;
@property (nonatomic, strong) Search_MV_dataModel *mv_data_Model;;
@property (nonatomic, strong) Search_MV_Data_mvListModel *MV_data_list_Model;

@end



#define faxianUrl @"http://api.dongting.com/frontpage/frontpage?location=0&version=1457062755278&app=ttpod&v=v8.1.4.2016011318&user_id=0&mid=iPhone8,1&f=f320&s=s310&imsi=&hid=&splus=9.0.2&active=1&net=2&openudid=00e5496604ba470f229b34929ee14d0eb192a8e8&idfa=2301BEB2-C95C-4AA8-A905-794EC5CED975&utdid=VrPuK0MAZLUDAGfmA9wxwiTF&alf=201200&bundle_id=com.ttpod.music&latitude=38.88258440926375&longtitude=121.5393131226384"

#define tuijianUrl @"http://api.dongting.com/misc/album/new?page=1&size=30&app=ttpod&v=v8.1.4.2016011318&user_id=0&mid=iPhone8,1&f=f320&s=s310&imsi=&hid=&splus=9.0.2&active=1&net=2&openudid=00e5496604ba470f229b34929ee14d0eb192a8e8&idfa=2301BEB2-C95C-4AA8-A905-794EC5CED975&utdid=VrPuK0MAZLUDAGfmA9wxwiTF&alf=201200&bundle_id=com.ttpod.music&latitude=38.88015919838485&longtitude=121.5415843296068"

// 轮播图点击事件（跳到歌曲列表）
#define LISTURL @"?app=ttpod&v=v8.1.4.2016011318&user_id=0&mid=iPhone8,1&f=f320&s=s310&imsi=&hid=&splus=9.0.2&active=1&net=2&openudid=00e5496604ba470f229b34929ee14d0eb192a8e8&idfa=2301BEB2-C95C-4AA8-A905-794EC5CED975&utdid=VrPuK0MAZLUDAGfmA9wxwiTF&alf=201200&bundle_id=com.ttpod.music&latitude=38.882548834006&longtitude=121.5391476381413"

@implementation HZ_Find_ViewController

-(void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   [self discoveryDataUrl];

    self.navigationController.navigationBar.translucent = NO;
//    self.view.backgroundColor = [UIColor colorWithWhite:0.951 alpha:1.000];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-liebiao"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(leftButton:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-sousuo-4"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightButton:)];
 
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"发现", @"动听", @"推荐"]];
    //  seg去掉边框
    _seg.selectedSegmentIndex = 0;
    _seg.tintColor = [UIColor clearColor];
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName: [UIColor colorWithRed:1.000 green:0.659 blue:0.347 alpha:1.000]};
    [_seg setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName: [UIColor colorWithWhite:0.290 alpha:1.000]};
    [_seg setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    self.seg.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
    [_seg addTarget:self action:@selector(seg:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = self.seg;
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    
    flow.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height - self.view.bounds.size.height / 10);
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0 ;
    flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;

 
    self.coll = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - self.view.bounds.size.height / 10) collectionViewLayout:flow];
  
    self.coll = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height - 64 - self.view.bounds.size.height / 10) collectionViewLayout:flow];

    [_coll registerClass:[HZ_Find_CollectionViewCell class] forCellWithReuseIdentifier:@"HZ_Find_CollectionViewCell"];
    _coll.pagingEnabled = YES;
    _coll.delegate = self;
    _coll.dataSource = self;
    [_coll registerClass:[HZ_jibuqiCollectionViewCell class] forCellWithReuseIdentifier:@"HZ_jibuqiCollectionViewCell"];
    [_coll registerClass:[HZJRecommendCVCell class] forCellWithReuseIdentifier:@"HZJRecommendCVCell"];
    
    [self.view addSubview:_coll];
    
    
    NSNotificationCenter *cellfenlei = [NSNotificationCenter defaultCenter];

    [cellfenlei addObserver:self selector:@selector(cellfenlei:) name:@"1" object:nil];

    
    NSNotificationCenter *cellfenleiTwo = [NSNotificationCenter defaultCenter];

    [cellfenleiTwo addObserver:self selector:@selector(cellfenleiTwo:) name:@"2" object:nil];

    
    NSNotificationCenter *cellfenleiThree = [NSNotificationCenter defaultCenter];
 
    [cellfenleiThree addObserver:self selector:@selector(cellfenleiThree:) name:@"3" object:nil];

    
    NSNotificationCenter *gengduo = [NSNotificationCenter defaultCenter];
 
    [gengduo addObserver:self selector:@selector(gengduo:) name:@"11" object:nil];
 
    
    NSNotificationCenter *toushitu = [NSNotificationCenter defaultCenter];
 
    [toushitu addObserver:self selector:@selector(toushitu:) name:@"5" object:nil];

    
    NSNotificationCenter *cslcenter = [NSNotificationCenter defaultCenter];
 
    [cslcenter addObserver:self selector:@selector(cslcenter:) name:@"Click" object:nil];
 
    
    NSNotificationCenter *hsplay = [NSNotificationCenter defaultCenter];
   
    [hsplay addObserver:self selector:@selector(hsplay:) name:@"6" object:nil];
    NSNotificationCenter *hsmorebutton = [NSNotificationCenter defaultCenter];
    
    [hsmorebutton addObserver:self selector:@selector(hsmorebutton:) name:@"hsmorebutton" object:nil];
    
    
//    发现数据请求
#pragma mark - 调用推荐页面数据请求
    [self getRecommendData];
    [_coll reloadData];
}

-(void)hsmorebutton:(NSNotification *)hsmorebutton{
    
    
    
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"温馨提示您的手机版本太低暂不支持" preferredStyle:  UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }]];
    
    [self presentViewController:alert animated:true completion:nil];
    
    
    
    
}


#pragma mark - 播放 模态 到 csl 页面
-(void)hsplay:(NSNotification *)hsplay{
        CSL_PlayerPage_ViewController *csl = [CSL_PlayerPage_ViewController playViewController];

    csl.array = hsplay.object;
    

       [self presentViewController:csl animated:YES completion:^{
       }];
}
#pragma mark - 大家在听中更多按钮的通知接受者
-(void)cslcenter:(NSNotification *)cslcenter{
    NSString *string = @"http://api.favorite.ttpod.com/favorite/song/plaza?f=384&os=4.4.2&alf=10002266&imei=864783024887313&random=0&from=android&resolution=720x1280&net=2&api_version=1.0&agent=none&v=v8.4.1.2016032512&utdid=VRu%2Blc%2FVSokDANuKLLSuSY%2B5&address=%E8%BE%BD%E5%AE%81%E7%9C%81%E5%A4%A7%E8%BF%9E%E5%B8%82%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA%E4%BA%94%E4%B8%80%E8%B7%AF314%E5%8F%B7&longitude=121.544426&user_id=0&latitude=38.883442&language=zh";
    HZJPlayListDetailViewController *playlis = [[HZJPlayListDetailViewController alloc] init];
    playlis.urlString = string;
    [self.navigationController pushViewController:playlis animated:YES];
}
//轮播图点击事件
-(void)toushitu:(NSNotification *)toushitu{
    NSString *str = [NSString stringWithFormat:@"%@", toushitu.object];
//    NSLog(@"%@", str);
    if (str.length > 10) {
        HZ_Wab_ViewController *wab = [[HZ_Wab_ViewController alloc] init];
        wab.string = toushitu.object;
        [self.navigationController pushViewController:wab animated:YES];
    }else {
        
        NSString *strUrl = [NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@%@", str, LISTURL];
        
        HZJPlayListDetailViewController *musicListVC = [[HZJPlayListDetailViewController alloc] init];
        musicListVC.urlString = strUrl;
        [self.navigationController pushViewController:musicListVC animated:YES];
        
    }
    
}
-(void)seg:(UISegmentedControl *)seg{
    _coll.contentOffset = CGPointMake(_seg.selectedSegmentIndex * self.view.bounds.size.width, 0);
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    _seg.selectedSegmentIndex = _coll.contentOffset.x / self.view.bounds.size.width;
}
// 排行榜
-(void)cellfenlei:(NSNotification *)cellfenlei{
    HS_Billboard_ViewController *billboard = [[HS_Billboard_ViewController alloc] init];
    [self.navigationController pushViewController:billboard animated:YES];
}
//歌单
-(void)cellfenleiTwo:(NSNotification *)cellfenleiTwo{
    HZ_playlist_ViewController *playlist = [[HZ_playlist_ViewController alloc] init];
    [self.navigationController pushViewController:playlist animated:YES];
}
#pragma mark - 歌手
- (void)jumpSinger {
    HZJ_SingerViewController *singerVC = [[HZJ_SingerViewController alloc] init];
    [self.navigationController pushViewController:singerVC animated:YES];
}


//电台
-(void)cellfenleiThree:(NSNotification *)cellfenleiThree{
    HZ_Radio_ViewController *radio = [[HZ_Radio_ViewController alloc] init];
    [self.navigationController pushViewController:radio animated:YES];
}
//更多
-(void)gengduo:(NSNotification *)gengduo{
    NSString *str = [NSString stringWithFormat:@"%@", gengduo.object];
    NSInteger age = str.integerValue;
    if (age == 2) {
        HZ_playlist_ViewController *playlist = [[HZ_playlist_ViewController alloc] init];
        [self.navigationController pushViewController:playlist animated:YES];
    } else if (age == 4){
        HZ_more_ViewController *more = [[HZ_more_ViewController alloc] init];
        [self.navigationController pushViewController:more animated:YES];
    } else if (age == 6) {
        CSL_MoreViewController *moreViewController = [[CSL_MoreViewController alloc] init];
        [self.navigationController pushViewController:moreViewController animated:YES];
    }
}

#pragma mark - 歌单item以及下一级 我是歌手  新歌新碟
- (void)jumpPlayList:(NSIndexPath *)indexPath {
    
    self.findModel = _discoveryArray[indexPath.section + 1];
    HS_DataArray_Model *model = _findModel.data[indexPath.item];
    
    if (indexPath.section == 2 || indexPath.section == 3) {
        HZJPlayListDetailViewController *listVC = [[HZJPlayListDetailViewController alloc] init];
        
        listVC.picUrl = model.picUrl;
        listVC.urlString = [NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@%@", model.diction.value, LISTURL];
        [self.navigationController pushViewController:listVC animated:YES];
    }
    
    if (indexPath.section == 4) {
        HZ_Wab_ViewController *webVC = [[HZ_Wab_ViewController alloc] init];
        webVC.string = model.diction.value;
        [self.navigationController pushViewController:webVC animated:YES];
    } else if (indexPath.section == 5) {
        HZJRecoDetailViewController *recodeVC = [[HZJRecoDetailViewController alloc] init];
        recodeVC.urlString = model.diction.value;
        recodeVC.picUrl = model.picUrl;
        recodeVC.detailStr = model.name;
        [self.navigationController pushViewController:recodeVC animated:YES];
    }
    else if (indexPath.section == 6) {
        
        NSNotificationCenter *hiddencsl = [NSNotificationCenter defaultCenter];
        [hiddencsl postNotificationName:@"hiddencsl" object:nil userInfo:nil];
        
        CSL_Player_ViewController *player_ViewController = [[CSL_Player_ViewController alloc] init];
        
        self.string = [NSString stringWithFormat:@"http://api.dongting.com/song/video/%@?utdid=VtwfsEIOP2gDAFg7QtbwosWn", model.diction.value];
        
        [HSAFNetTool GETNetWithUrl:_string body:nil headerFile:nil response:HSJSON success:^(id result) {
            
            self.mv_data_Model = [[Search_MV_dataModel alloc] initWithDataSource:result[@"data"]];
            Search_MV_Data_mvListModel *mv_Data_mvListModel = _mv_data_Model.mvList[0];
            
            player_ViewController.mv_Data_mvListModel = mv_Data_mvListModel;
            
            [self presentViewController:player_ViewController animated:YES completion:^{}];
        } failure:^(NSError *error) {
            
        }];
       
        
    } else if (indexPath.section == 7) {
        if (model.diction.type.intValue == 1) {
            HZ_Wab_ViewController *webVC = [[HZ_Wab_ViewController alloc] init];
            webVC.string = model.diction.value;
            [self.navigationController pushViewController:webVC animated:YES];
        } else if (model.diction.type.intValue == 19) {
            
            HZJRecoDetailViewController *recodeVC = [[HZJRecoDetailViewController alloc] init];
            recodeVC.urlString = model.diction.value;
            recodeVC.picUrl = model.picUrl;
            recodeVC.detailStr = model.name;
            [self.navigationController pushViewController:recodeVC animated:YES];
        }
    }
    else if (indexPath.section == 8) {
        HZ_Wab_ViewController *webVC = [[HZ_Wab_ViewController alloc] init];
        webVC.string = model.diction.value;
        [self.navigationController pushViewController:webVC animated:YES];
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//    推荐界面
    if (indexPath.item == 2) {
        HZJRecommendCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZJRecommendCVCell" forIndexPath:indexPath];
        cell.dataSource = self.tuijianDataSource;
        cell.delegate = self;
        return cell;
    } else if (indexPath.row == 0) {
        
        HZ_Find_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZ_Find_CollectionViewCell" forIndexPath:indexPath];
        cell.findArray = self.discoveryArray;
        cell.delegate = self;
        return cell;
 
        
    } else {
        
        HZ_jibuqiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZ_jibuqiCollectionViewCell" forIndexPath:indexPath];
        
        
        return cell;
        
        
        
    }
}
#pragma mark - 抽屉方法
//抽屉按钮
-(void)leftButton:(UIButton *)button{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];    
    //    如果抽屉关闭  就打开
    if (tempAppDelegate.leftSlideVC.closed)
    {
        [tempAppDelegate.leftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.leftSlideVC closeLeftView];
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.leftSlideVC setPanEnabled:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.leftSlideVC setPanEnabled:NO];
}
#pragma mark - 点击搜索按钮
//搜索按钮
-(void)rightButton:(UIButton *)button{
    HZ_Search_ViewController *search = [[HZ_Search_ViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}
#pragma mark - 发现数据请求
-(void)discoveryDataUrl {
        [HSAFNetTool GETNetWithUrl:faxianUrl body:nil headerFile:nil response:HSJSON success:^(id result) {
             self.discoveryArray = [NSMutableArray arrayWithCapacity:0];
            for (NSDictionary *dic in result[@"data"]) {
                self.findModel = [[HZ_Find_Model alloc] initWithDataSource:dic];
                [self.discoveryArray addObject:self.findModel];
            }
            [self.coll reloadData];
        } failure:^(NSError *error) {
        }];
}
#pragma mark - 推荐界面数据请求
- (void)getRecommendData {
    [HSAFNetTool GETNetWithUrl:tuijianUrl body:nil headerFile:nil response:HSJSON success:^(id result) {
        self.tuijianDataSource = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in result[@"data"]) {
            HZJTuijianModel *tuijianModel = [[HZJTuijianModel alloc] initWithDataSource:dic];
            [self.tuijianDataSource addObject:tuijianModel];
        }
        if (self.tuijianDataSource.count > 0) {
            [_coll reloadData];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}
#pragma mark - 推荐协议传值（传递点击那一个cell）
- (void)JRecommendSendIndex:(NSIndexPath *)indexPath {
    HZJTuijianModel *tuijianModel = self.tuijianDataSource[indexPath.row];
    HZJRecoDetailViewController *recommDetailVC = [[HZJRecoDetailViewController alloc] init];
    recommDetailVC.urlString = [NSString stringWithFormat:@"%ld", (long)tuijianModel.albumId];
    recommDetailVC.detailStr = tuijianModel.name;
    recommDetailVC.picUrl = tuijianModel.picUrl;
    [self.navigationController pushViewController:recommDetailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
